<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="name" label="名称" required>
        <uni-easyinput placeholder="商品名称" v-model="formData.name" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="category_id" label="分类ID" required>
        <uni-easyinput placeholder="分类 id，参考`opendb-mall-categories`表" v-model="formData.category_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="original_good_id" label="原商品ID">
        <uni-easyinput placeholder="原商品id，参考商品表" v-model="formData.original_good_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="goods_thumb" label="缩略图地址">
        <uni-easyinput placeholder="商品缩略图，用于在列表或搜索结果中预览显示" v-model="formData.goods_thumb" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="merchant_id" label="转售商户ID">
        <uni-easyinput placeholder="转售商户id，参考`intangible_merchant`表" v-model="formData.merchant_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="remain_count" label="库存数量">
        <uni-easyinput placeholder="库存数量" type="number" v-model="formData.remain_count"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="price" label="转售价格">
        <uni-easyinput placeholder="转售价格" v-model="formData.price" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="original_price" label="原购买价格">
        <uni-easyinput placeholder="原购买价格" v-model="formData.original_price" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="keywords" label="关键字">
        <uni-easyinput placeholder="商品关键字即标签，多个关键字请用逗号隔开" v-model="formData.keywords" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="condition_status" label="商品成色" required>
        <uni-data-checkbox v-model="formData.condition_status" :localdata="formOptions.condition_status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="resale_reason" label="转售原因">
        <uni-easyinput placeholder="转售原因说明" v-model="formData.resale_reason" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="condition_desc" label="成色描述">
        <uni-easyinput placeholder="商品成色详细描述" v-model="formData.condition_desc" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="resale_imgs" label="转售实拍图">
        <uni-data-checkbox :multiple="true" v-model="formData.resale_imgs"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="favorite_count" label="收藏次数">
        <uni-easyinput placeholder="收藏次数" type="number" v-model="formData.favorite_count"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="status" label="转售状态">
        <uni-data-checkbox v-model="formData.status" :localdata="formOptions.status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="add_date" label="上架时间">
        <uni-datetime-picker return-type="timestamp" v-model="formData.add_date"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="postfee_template_id" label="邮费模板ID">
        <uni-easyinput placeholder="关联的邮费模板ID，参考intangible_postfee表" v-model="formData.postfee_template_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="free_shipping" label="是否包邮">
        <switch @change="binddata('free_shipping', $event.detail.value)" :checked="formData.free_shipping"></switch>
      </uni-forms-item>
      <uni-forms-item name="weight" label="商品重量">
        <undefined v-model="formData.weight"></undefined>
      </uni-forms-item>
      <uni-forms-item name="seller_note" label="卖家备注">
        <uni-easyinput placeholder="卖家备注说明" v-model="formData.seller_note" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="type" label="售卖类型">
        <uni-easyinput placeholder="售卖类型 1为二手转售" type="number" v-model="formData.type"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="hashcode" label="上链哈希码">
        <uni-easyinput placeholder="上链哈希码" v-model="formData.hashcode" trim="both"></uni-easyinput>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/intangible_resalegoods.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'intangible_resalegoods';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "name": "",
        "category_id": "",
        "original_good_id": "",
        "goods_thumb": "",
        "merchant_id": "",
        "remain_count": null,
        "price": "",
        "original_price": "",
        "keywords": "",
        "condition_status": null,
        "resale_reason": "",
        "condition_desc": "",
        "resale_imgs": [],
        "favorite_count": 0,
        "status": 0,
        "add_date": null,
        "postfee_template_id": "",
        "free_shipping": false,
        "weight": null,
        "seller_note": "",
        "type": 1,
        "hashcode": ""
      }
      return {
        formData,
        formOptions: {
          "condition_status_localdata": [
            {
              "text": "全新未拆",
              "value": 0
            },
            {
              "text": "全新仅拆",
              "value": 1
            },
            {
              "text": "9.9成新",
              "value": 2
            },
            {
              "text": "9成新",
              "value": 3
            },
            {
              "text": "8成新",
              "value": 4
            },
            {
              "text": "7成新",
              "value": 5
            },
            {
              "text": "6成新",
              "value": 6
            },
            {
              "text": "5成新",
              "value": 7
            },
            {
              "text": "4成新",
              "value": 8
            },
            {
              "text": "3成新",
              "value": 9
            },
            {
              "text": "2成新",
              "value": 10
            },
            {
              "text": "1成新",
              "value": 11
            }
          ],
          "status_localdata": [
            {
              "text": "在售",
              "value": 0
            },
            {
              "text": "售罄",
              "value": 1
            },
            {
              "text": "已下架",
              "value": 2
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
